package com.fjp.heartguard.ui.report

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fjp.heartguard.app.Constants
import com.fjp.heartguard.app.base.BaseActivity
import com.fjp.heartguard.app.data.bean.ReportsItem
import com.fjp.heartguard.app.data.bean.ReportsType
import com.fjp.heartguard.app.eventViewModel
import com.fjp.heartguard.app.ext.init
import com.fjp.heartguard.databinding.ActivityHistoryReportBinding
import com.fjp.heartguard.databinding.ActivitySingleAllReportBinding
import com.fjp.heartguard.ui.report.adapter.HistoryReportAdapter
import com.fjp.heartguard.ui.report.adapter.ReportAdapter
import com.fjp.heartguard.viewmodel.HistoryReportViewModel
import com.fjp.heartguard.viewmodel.SingleAllReportViewModel
import me.hgj.jetpackmvvm.ext.parseState

class SingleAllReportActivity : BaseActivity<SingleAllReportViewModel, ActivitySingleAllReportBinding>() {
    var hasMore = false
    val reportAdapter = ReportAdapter(arrayListOf<ReportsItem>()) {it,index->
        val intent = Intent(this, ReportDetailActivity::class.java)
        intent.putExtra(Constants.Intent_OLDER_ID, it.older_id)
        intent.putExtra(Constants.INTENT_REPORT_ID, it.id)
        startActivity(intent)

    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        mDatabind.click = ClickProxy()
        mViewModel.title.set("测评历史")
        mViewModel.olderId = intent.getIntExtra(Constants.Intent_OLDER_ID,0)
        mViewModel.getQuestionnaireList()

        mDatabind.recyclerView.init(LinearLayoutManager(this, RecyclerView.VERTICAL, false), reportAdapter)
        mDatabind.refreshLayout.setOnRefreshListener {
            mViewModel.page = 1
            //发起网络请求获取数据
            mDatabind.refreshLayout.isRefreshing = false
            mViewModel.getQuestionnaireList()
        }
        mDatabind.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                // 获取LinearLayoutManager
                val layoutManager = recyclerView.layoutManager as LinearLayoutManager?

                // 获取最后一个完全显示的item的position
                val lastVisibleItemPosition = layoutManager!!.findLastCompletelyVisibleItemPosition()
                val totalItemCount = layoutManager!!.itemCount

                // 判断是否滑动到底部
                if (lastVisibleItemPosition + 1 >= totalItemCount && hasMore) {
                    // 执行滑动到底部的操作，例如加载更多数据
                    mViewModel.getQuestionnaireList()
                }
            }
        });

    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.reports.observeForever {
            parseState(it, {
                if (mViewModel.page == 1) {
                    reportAdapter.data.clear()
                }
                hasMore = it.data.size == mViewModel.pageSize
                reportAdapter.addData(it.data)
                if (reportAdapter.data.size == 0) {
                    mDatabind.emptyLl.visibility = View.VISIBLE
                } else {
                    mDatabind.emptyLl.visibility = View.GONE
                }
                mViewModel.page++
            }, {
                it.printStackTrace()
            })
        }
    }

    inner class ClickProxy {
        fun clickRefresh() {
            mViewModel.page = 1
            mViewModel.getQuestionnaireList()
        }
    }

}